﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;

using NUnit.Framework;

using ERPStore.Extensions;

using Microsoft.Practices.Unity;


namespace ERPStore.Exporter.Tests
{
	[TestFixture]
	public class ExportTexts : ERPStore.Tests.TestBase
	{
		[SetUp]
		public override void Initialize()
		{
			base.Initialize();

			Services.ShopzillaService.GererateTaxonomy();

			while (true)
			{
				var pg = CloudSoft.Workflows.ProgressReporterCollector.Get("7C968916-54A8-4544-9413-E1FC1D043765");
				if (pg == null)
				{
					break;
				}
				System.Threading.Thread.Sleep(1 * 1000);
			}
		}

		[Test]
		public void CreateFiles()
		{
			var parameters = new Dictionary<string, object>();

			var cherchons = new ERPStore.Exporter.Providers.CherchonsExport();
			cherchons.Settings.AllowStockEmpty = true;
			var achetezFacile = new ERPStore.Exporter.Providers.AchetezFacileExport();
			achetezFacile.Settings.AllowStockEmpty = true;
			var googleShopping = new ERPStore.Exporter.Providers.GoogleShoppingExport();
			googleShopping.Settings.AllowStockEmpty = true;
			var idealo = new ERPStore.Exporter.Providers.IdealoExport();
			idealo.Settings.AllowStockEmpty = true;
			var leguide = new ERPStore.Exporter.Providers.LeguideExport();
			leguide.Settings.AllowStockEmpty = true;
			var shopzilla = new ERPStore.Exporter.Providers.ShopzillaExport();
			shopzilla.Settings.AllowStockEmpty = true;
			var sitemap = new ERPStore.Exporter.Providers.SitemapExport();
			sitemap.Settings.AllowStockEmpty = true;
			var hellopro = new ERPStore.Exporter.Providers.HelloproExport();
			hellopro.Settings.AllowStockEmpty = true;
			var netaffiliation = new ERPStore.Exporter.Providers.NetAffiliationExport();
			netaffiliation.Settings.AllowStockEmpty = true;
			var twenga = new ERPStore.Exporter.Providers.TwengaExport();
			twenga.Settings.AllowStockEmpty = true;
			var criteo = new ERPStore.Exporter.Providers.CriteoExport();
			criteo.Settings.AllowStockEmpty = true;
			var touslesprix = new ERPStore.Exporter.Providers.TouslesprixExport();
			touslesprix.Settings.AllowStockEmpty = true;


			parameters.Add("ExportList", new List<Providers.IExport>() 
			{ 
				cherchons,
				achetezFacile,
				googleShopping,
				idealo,
				leguide,
				shopzilla,
				sitemap,
				hellopro,
				netaffiliation,
				twenga,
				criteo,
				touslesprix,
			});

			parameters.Add("ForceExport", true);

			var wi = new WorkflowInvoker(new Workflows.GenerateCatalogs());
			var output = wi.Invoke(parameters);

			var fileInfo = new System.IO.FileInfo(cherchons.Settings.FileName);
			Assert.IsTrue(fileInfo.Exists);
			Assert.AreNotEqual(0, cherchons.Settings.ProductCount);
			fileInfo = new System.IO.FileInfo(achetezFacile.Settings.FileName);
			Assert.IsTrue(fileInfo.Exists);
			Assert.AreNotEqual(0, achetezFacile.Settings.ProductCount);
			fileInfo = new System.IO.FileInfo(googleShopping.Settings.FileName);
			Assert.IsTrue(fileInfo.Exists);
			Assert.AreNotEqual(0, googleShopping.Settings.ProductCount);
			fileInfo = new System.IO.FileInfo(idealo.Settings.FileName);
			Assert.IsTrue(fileInfo.Exists);
			Assert.AreNotEqual(0, idealo.Settings.ProductCount);
			fileInfo = new System.IO.FileInfo(leguide.Settings.FileName);
			Assert.IsTrue(fileInfo.Exists);
			Assert.AreNotEqual(0, leguide.Settings.ProductCount);
			fileInfo = new System.IO.FileInfo(shopzilla.Settings.FileName);
			Assert.IsTrue(fileInfo.Exists);
			Assert.AreNotEqual(0, shopzilla.Settings.ProductCount);
			fileInfo = new System.IO.FileInfo(sitemap.Settings.FileName);
			Assert.IsTrue(fileInfo.Exists);
			Assert.AreNotEqual(0, sitemap.Settings.ProductCount);
			fileInfo = new System.IO.FileInfo(hellopro.Settings.FileName);
			Assert.IsTrue(fileInfo.Exists);
			Assert.AreNotEqual(0, hellopro.Settings.ProductCount);
			fileInfo = new System.IO.FileInfo(netaffiliation.Settings.FileName);
			Assert.IsTrue(fileInfo.Exists);
			Assert.AreNotEqual(0, netaffiliation.Settings.ProductCount);
			fileInfo = new System.IO.FileInfo(twenga.Settings.FileName);
			Assert.IsTrue(fileInfo.Exists);
			Assert.AreNotEqual(0, twenga.Settings.ProductCount);
			fileInfo = new System.IO.FileInfo(criteo.Settings.FileName);
			Assert.IsTrue(fileInfo.Exists);
			Assert.AreNotEqual(0, criteo.Settings.ProductCount);
			fileInfo = new System.IO.FileInfo(touslesprix.Settings.FileName);
			Assert.IsTrue(fileInfo.Exists);
			Assert.AreNotEqual(0, touslesprix.Settings.ProductCount);
		}

		[Test]
		public void Math_Cherchons_Source()
		{
			var cherchonsSource = new Models.LeadSources.CherchonsSource();
			var sourceUri = new Uri("http://shopping.cherchons.com/cgi/cgi.cgi?action=visitSite&&level=0&account=www.quincaillerie.pro&url=http%3A%2F%2Fwww.quincaillerie.pro%2Fcp%2Ftrk%2FFcHj_n9TjMjsfsZJLULG0Ddw9LIYcwaZNp9aupdi6Crckf6GfI_QxDIumQ72mf_seeDkxYkIW0WwFk4pFx75rgWcPyf3yBm8&prodId=P067RhXfV8VMyf-PNeWTvQ&parameter=&titre=Quincaillerie%20Pro%3ANettoyant%20vitres%20500%20ml%20RM%20500%20%3A%20Karcher%2062953750:4%2C96&http_referer=http%3A%2F%2Fshopping.cherchons.com%2Fr%2Fnettoyant-vitre-karcher.html");
			var landingUri = new Uri("http://www.test.com/?estid=cherchons.com");
			string keywords = null;
			string subHost = null;
			decimal amount = 0;
			var result = cherchonsSource.Match(sourceUri, landingUri, out keywords, out subHost, out amount);

			Assert.AreEqual(result, true);
			Assert.AreEqual(keywords, "nettoyant vitre karcher");

			sourceUri = new Uri("http://shopping.cherchons.com/cgi/cgi.cgi?action=visitSite&&level=0&account=www.quincaillerie.pro&url=http%3A%2F%2Fwww.quincaillerie.pro%2Fcp%2Ftrk%2F5ZDfKrYkdbtyAo4ctlblihmKSwoMIGy1FU-gbjt_FTGNHGIppp4SrPGn_URp9anJmV8yhiROAeANiX4oR7jfKPRPQThdThZi&prodId=U6HkCwE-a-PVfDNAFn-idQ&parameter=&titre=Quincaillerie%20Pro%3APistolet%20de%20sablage%20avec%20r%E9servoir%20de%2010%20litres%20%3A%20Rodcraft%208112:118%2C40&http_referer=http%3A%2F%2Fshopping.cherchons.com%2Fr%2Fpistolet-de-sablage.html");

			result = cherchonsSource.Match(sourceUri, landingUri, out keywords, out subHost, out amount);

			Assert.AreEqual(keywords, "pistolet de sablage");

			sourceUri = new Uri("http://www.source.com");

			result = cherchonsSource.Match(sourceUri, landingUri, out keywords, out subHost, out amount);

			Assert.AreEqual(keywords, string.Empty);
		}
	}
}
